unit LoginForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, uADStanIntf,
  uADStanOption, uADStanError, uADGUIxIntf, uADPhysIntf, uADStanDef,
  uADStanPool, uADStanAsync, uADPhysManager, uADPhysIB, Data.DB, uADCompClient,
  IBDatabaseINI, uADStanParam, uADDatSManager, uADDAptIntf, uADDAptManager,
  uADCompDataSet, uADGUIxFormsWait, uADCompGUIx, Vcl.Grids, System.SysUtils,
  Vcl.DBGrids, UDF, System.UITypes, uADPhysMySQL, System.IniFiles;

type
  TfmLogin = class(TForm)
    txtUsername: TLabeledEdit;
    txtPassword: TLabeledEdit;
    btnLogin: TButton;
    btnCancel: TButton;
    ADConnection1: TADConnection;
    ADQUserPass: TADQuery;
    ADGUIxWaitCursor1: TADGUIxWaitCursor;
    ADTable1: TADTable;
    DataSource1: TDataSource;
    ADPhysMySQLDriverLink1: TADPhysMySQLDriverLink;
    ADPhysIBDriverLink1: TADPhysIBDriverLink;
    procedure FormCreate(Sender: TObject);
    procedure btnLoginClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure txtUsernameKeyPress(Sender: TObject; var Key: Char);
    procedure txtPasswordKeyPress(Sender: TObject; var Key: Char);
    procedure FormActivate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmLogin: TfmLogin;

implementation

uses
  MainForm;

{$R *.dfm}

procedure TfmLogin.FormActivate(Sender: TObject);
begin
  txtUsername.SetFocus;
end;

procedure TfmLogin.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
//  CanClose := True;
//  FreeAndNil(fmLogin);
end;

procedure TfmLogin.FormCreate(Sender: TObject);
var
  ADPhysDriverLink: TADPhysDriverLink;
  DriverID: string;
begin
  DriverID := DBDriverID;
  if DriverID = 'Firebird' then
    ADPhysDriverLink := ADPhysIBDriverLink1;
  if DriverID = 'MySQL' then
    ADPhysDriverLink := ADPhysMySQLDriverLink1;
  SetDBInfo(ADConnection1, ADPhysDriverLink);
  ADTable1.Active := True;
end;

procedure TfmLogin.txtPasswordKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = Char(vkReturn) then
    btnLogin.Click;
end;

procedure TfmLogin.txtUsernameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = Char(vkReturn) then
    txtPassword.SetFocus;
end;

procedure TfmLogin.btnCancelClick(Sender: TObject);
begin
//  Application.Terminate;
  fmMain.Login1.Enabled := True;
  fmMain.Logout1.Enabled := False;
  Close;
end;

procedure TfmLogin.btnLoginClick(Sender: TObject);
begin
  if txtPassword.Text = '' then
  begin
    ShowMessage('Password can''t be empty');
    Exit;
  end;
  ADQUserPass.Params.ParamByName('Username').Value := Trim(txtUsername.Text);
  ADQUserPass.Params.ParamByName('Password').Value := MD5(Trim(txtPassword.Text));
  if not ADQUserPass.Prepared then
    ADQUserPass.Prepare;
  ADQUserPass.Active := True;
  if ADQUserPass.RecordCount > 0 then
  begin
    fmMain.Show;
    fmMain.Enabled := True;
    fmMain.Login1.Enabled := False;
    fmMain.Logout1.Enabled := True;
    fmMain.Administration1.Visible := True;
    fmMain.Finance1.Visible := True;
    fmMain.StatusBar1.Panels[1].Text := txtUsername.Text;
    fmMain.StatusBar1.Panels[3].Text := FormatDateTime('yyyy-mm-dd hh:nn:ss', Now);
    fmMain.Timer1.Enabled := True;
    fmMain.LoggedIn := True;
    txtUsername.Text := '';
    txtPassword.Text := '';
    Close;
  end else begin
    ShowMessage('Username or Password is wrong!');
  end;
end;

end.
